package com.woniu.userservice.dao;

import com.woniu.userservice.entity.Positions;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PositionsDao {
    Positions queryById(Integer id);

    List<Positions> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);
    
    List<Positions> findAll();

    List<Positions> queryAll(Positions positions);

    int insert(Positions positions);

    int update(Positions positions);

    int deleteById(Integer id);

    /**
     *
     * @param userid
     * @return 查询是否存在默认地址
     */
    @Select("select * from positions where userId = #{userid} and isDefault=1 ")
    List<Positions> findById( Integer userid);

    /**
     *
     * @param userid
     * @param phonenumber
     * @param receivername
     * @param postnumber
     * @param positioninfo
     * @param isdefault
     * 插入默认地址
     */
    @Insert("insert into positions(userId, phoneNumber, receiverName, postNumber, positionInfo, isDefault) values (#{userid}, #{phonenumber}, #{receivername}, #{postnumber}, #{positioninfo}, #{isdefault})")
    void insertAll(@Param("userid") Integer userid,@Param("phonenumber") String phonenumber, @Param("receivername") String receivername, @Param("postnumber")Integer postnumber,@Param("positioninfo") String positioninfo, @Param("isdefault")Object isdefault);

    /**
     * 强制设置默认地址
     */
    @Update("update positions set isDefault = 1")
    void updateByIsdefault();

    /**
     *
     * @param uid
     * @return 查询该用户所有的地址
     */
    @Select("select * from positions where userId = #{uid}")
    List<Positions> findByUid(Integer uid);

    /**
     *
     * @param uid
     * @return 用户的默认地址
     */
    @Select("select * from positions where userId =#{uid} and isDefault = 0")
    List<Positions> findByOneUid(Integer uid);

    /**
     *
     * @param id
     * @param userid
     * @return 删除用户默认地址
     */
    @Delete("delete  from positions where userId = #{userid} and id = #{id} and isDefault !=1 ")
    Boolean deleteByUserId(@Param("id") Integer id, @Param("userid") Integer userid);

    /**
     * 取消原来的默认地址
     */
    @Update("update positions set isDefault = 0 where userId = #{userid} and isDefault = 1 ")
    Boolean updateByIsdefaultOne(Integer userid);

}